home *** CD-ROM | disk | FTP | other *** search
/ Champak 145 / (Vol 145) Dec 21 2011.iso / Games / kupidon-strike.swf / scripts / __Packages / com / meychi / ascrypt / RC4.as
Encoding:
Text File  |  2011-12-21  |  3.3 KB  |  110 lines

  1. class com.meychi.ascrypt.RC4
  2. {
  3.    static var sbox = new Array(255);
  4.    static var mykey = new Array(255);
  5.    function RC4()
  6.    {
  7.    }
  8.    static function encrypt(src, key)
  9.    {
  10.       var _loc3_ = com.meychi.ascrypt.RC4.strToChars(src);
  11.       var _loc1_ = com.meychi.ascrypt.RC4.strToChars(key);
  12.       var _loc2_ = com.meychi.ascrypt.RC4.calculate(_loc3_,_loc1_);
  13.       return com.meychi.ascrypt.RC4.charsToHex(_loc2_);
  14.    }
  15.    static function decrypt(src, key)
  16.    {
  17.       var _loc3_ = com.meychi.ascrypt.RC4.hexToChars(src);
  18.       var _loc1_ = com.meychi.ascrypt.RC4.strToChars(key);
  19.       var _loc2_ = com.meychi.ascrypt.RC4.calculate(_loc3_,_loc1_);
  20.       return com.meychi.ascrypt.RC4.charsToStr(_loc2_);
  21.    }
  22.    static function initialize(pwd)
  23.    {
  24.       var _loc2_ = 0;
  25.       var _loc3_ = undefined;
  26.       var _loc4_ = pwd.length;
  27.       var _loc1_ = 0;
  28.       while(_loc1_ <= 255)
  29.       {
  30.          com.meychi.ascrypt.RC4.mykey[_loc1_] = pwd[_loc1_ % _loc4_];
  31.          com.meychi.ascrypt.RC4.sbox[_loc1_] = _loc1_;
  32.          _loc1_ = _loc1_ + 1;
  33.       }
  34.       _loc1_ = 0;
  35.       while(_loc1_ <= 255)
  36.       {
  37.          _loc2_ = (_loc2_ + com.meychi.ascrypt.RC4.sbox[_loc1_] + com.meychi.ascrypt.RC4.mykey[_loc1_]) % 256;
  38.          _loc3_ = com.meychi.ascrypt.RC4.sbox[_loc1_];
  39.          com.meychi.ascrypt.RC4.sbox[_loc1_] = com.meychi.ascrypt.RC4.sbox[_loc2_];
  40.          com.meychi.ascrypt.RC4.sbox[_loc2_] = _loc3_;
  41.          _loc1_ = _loc1_ + 1;
  42.       }
  43.    }
  44.    static function calculate(plaintxt, psw)
  45.    {
  46.       com.meychi.ascrypt.RC4.initialize(psw);
  47.       var _loc1_ = 0;
  48.       var _loc2_ = 0;
  49.       var _loc9_ = new Array();
  50.       var _loc7_ = undefined;
  51.       var _loc5_ = undefined;
  52.       var _loc6_ = undefined;
  53.       var _loc3_ = 0;
  54.       if(_loc3_ < plaintxt.length)
  55.       {
  56.          _loc1_ = (_loc1_ + 1) % 256;
  57.          _loc2_ = (_loc2_ + com.meychi.ascrypt.RC4.sbox[_loc1_]) % 256;
  58.          _loc5_ = com.meychi.ascrypt.RC4.sbox[_loc1_];
  59.          com.meychi.ascrypt.RC4.sbox[_loc1_] = com.meychi.ascrypt.RC4.sbox[_loc2_];
  60.          com.meychi.ascrypt.RC4.sbox[_loc2_] = _loc5_;
  61.       }
  62.       return _loc9_;
  63.    }
  64.    static function charsToHex(chars)
  65.    {
  66.       var _loc4_ = new String("");
  67.       var _loc3_ = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  68.       var _loc1_ = 0;
  69.       while(_loc1_ < chars.length)
  70.       {
  71.          _loc4_ += _loc3_[chars[_loc1_] >> 4] + _loc3_[chars[_loc1_] & 15];
  72.          _loc1_ = _loc1_ + 1;
  73.       }
  74.       return _loc4_;
  75.    }
  76.    static function hexToChars(hex)
  77.    {
  78.       var _loc3_ = new Array();
  79.       var _loc1_ = hex.substr(0,2) != "0x" ? 0 : 2;
  80.       while(_loc1_ < hex.length)
  81.       {
  82.          _loc3_.push(parseInt(hex.substr(_loc1_,2),16));
  83.          _loc1_ += 2;
  84.       }
  85.       return _loc3_;
  86.    }
  87.    static function charsToStr(chars)
  88.    {
  89.       var _loc3_ = new String("");
  90.       var _loc1_ = 0;
  91.       while(_loc1_ < chars.length)
  92.       {
  93.          _loc3_ += String.fromCharCode(chars[_loc1_]);
  94.          _loc1_ = _loc1_ + 1;
  95.       }
  96.       return _loc3_;
  97.    }
  98.    static function strToChars(str)
  99.    {
  100.       var _loc3_ = new Array();
  101.       var _loc1_ = 0;
  102.       while(_loc1_ < str.length)
  103.       {
  104.          _loc3_.push(str.charCodeAt(_loc1_));
  105.          _loc1_ = _loc1_ + 1;
  106.       }
  107.       return _loc3_;
  108.    }
  109. }
  110.